import {Service} from 'egg';

class NewsService extends Service{
    /**
     * 添加新闻
     * @param param 
     */
    public async createNews(param:any){
        try {
            //创建主键
            param.news_id = this.ctx.helper.genUUID();
            param.is_delete = '0';
            const result: any = await this.ctx.model.News.create(param);
            if (!result) {
                throw new Error(JSON.stringify({ code: 2, msg: '创建失败' }));
            }
        } catch (e) {
            this.ctx.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 删除新闻
     * @param param 
     */
    public async deleteNews(param:any){
        try {
            const result: any = await this.ctx.model.News.update({ is_delete: '1' }, { where: { news_id: param.news_id } });
            if (result[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '删除失败' }));
            }
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 修改新闻
     * @param param 
     */
    public async updateNews(param: any) {
        try {
            const { news_id, news_title,news_summary, news_detail } = param;
            const result: any = await this.ctx.model.News.update({ news_title,news_summary, news_detail }, { where: { news_id, is_delete: '0' } });
            if (result[0] <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '修改失败' }));
            }
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 查找新闻
     * @param param 
     */
    public async searchNews(param: any) {
        try {
            let { news_title } = param;
            const actulParam = {};
            let queryStr = `select news_id,news_title,news_summary,created_at from t_news where is_delete='0'`;
            if (news_title) {
                queryStr += ` and news_title like :news_title`;
                actulParam['news_title'] = '%' + news_title + '%';
            }
            queryStr += ` order by created_at desc`;
            const result: any = await this.ctx.model.query(queryStr, {
                type: this.ctx.model.QueryTypes.SELECT,
                replacements: actulParam
            })
            if (result.length <= 0) {
                throw new Error(JSON.stringify({ code: 2, msg: '没有数据' }));
            }
            return result;
        } catch (e) {
            this.logger.error(e);
            throw new Error(e.message);
        }
    }

    /**
     * 根据Id查找新闻详情
     * @param param 
     */
    public async searchNewsById(param:any){
        try{
            const result:any=await this.ctx.model.News.findOne({attributes:['news_title','news_summary','news_detail','created_at'],where:{news_id:param.news_id,is_delete:'0'}});
            if(!result){
                throw new Error(JSON.stringify({code:2,msg:'查找失败'}));
            }
            return result;
        }catch(e){
            this.logger.error(e);
            throw new Error(e.message);
        }
    }
}
export default NewsService;